//
//  MatchCell.m
//  体育
//
//  Created by  on 2022/10/24.
//

#import "MatchCell.h"
#import "RankItem.h"
#define s(m) [MatchCell s:(m)]
@interface MatchCell()
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView1;
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView2;
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView3;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *collectionVIew3H;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *collectionView1W;
@property (strong, nonatomic)  NSArray *array1;
@property (strong, nonatomic)  NSArray *array2;
@property (strong, nonatomic)  NSArray *array3;
@property (strong, nonatomic)  NSArray *array4;
@property (strong, nonatomic)  NSArray *array5;
@property (strong, nonatomic)  NSArray *array6;
@property (strong, nonatomic)  NSArray *array7;
@property (strong, nonatomic)  NSArray *array8;
@property (strong, nonatomic)  NSArray *array9;
@end
@implementation MatchCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [self.collectionView1 registerNib:[RankItem nib] forCellWithReuseIdentifier:[RankItem ID]];
    [self.collectionView2 registerNib:[RankItem nib] forCellWithReuseIdentifier:[RankItem ID]];
    [self.collectionView3 registerNib:[RankItem nib] forCellWithReuseIdentifier:[RankItem ID]];
   self.collectionView1W.constant = ceil((SCREEN_WIDTH - 24) / 8.0) * 5;
    
}

- (void)setModel:(Record *)model{
    _model = model;
    self.collectionVIew3H.constant = (model.isbasketball ? 8:5) * 26 + 20 + 30;

    self.array1 =  model.isbasketball ? @[@"",@"一",@"二",@"三",@"四"] :  @[@"",@"上",@"下"];
    self.array2 =  model.isbasketball ? @[@"主",s(model.fir_score_home),s(model.sec_score_home),s(model.thrd_score_home),s(model.fth_score_home)] :  @[@"主",s(model.fir_score_home),s(model.sec_score_home)];
    self.array3 =  model.isbasketball ? @[@"主",s(model.fir_score_from),s(model.sec_score_from),s(model.thrd_score_from),s(model.fth_score_from)] :  @[@"主",s(model.fir_score_from),s(model.sec_score_from)];
    self.array4 = @[@"",@"分差",@"总分"];
    
    self.array5 = @[@"半",s(model.top_half_score_home - model.top_half_score_from),s(model.score_home)];
    self.array6 = @[@"全",s(model.top_half_score_from - model.top_half_score_home),s(model.score_from)];
    
    
    ///
    if (model.isbasketball) {
        self.array7 = @[[NSString stringWithFormat:@"%d/%d(%0.1f%%)",(int)model.total_hit_home
        ,(int)model.total_shoot_home,model.total_hit_home * 100.0/model.total_shoot_home],
                        [NSString stringWithFormat:@"%d/%d(%0.1f%%)",(int)model.three_hit_home
                        ,(int)model.three_shoot_home,model.three_hit_home * 100.0/model.three_shoot_home],
                        [NSString stringWithFormat:@"%d/%d(%0.1f%%)",(int)model.penalty_hit_home
                         ,(int)model.penalty_shoot_home,model.penalty_hit_home * 100.0/model.penalty_shoot_home],
                        s(model.REB_home),s(model.AST_home),s(model.STL_home),s(model.BLK_home),s(model.FO_home),s(model.MIS_home)];
        self.array8 = @[@"命中/投篮",@"命中/3分",@"命中/罚球",@"篮板",@"助攻",@"抢断",@"盖帽",@"犯规",@"总失误"];
        
        self.array9 = @[[NSString stringWithFormat:@"%d/%d(%0.1f%%)",(int)model.total_hit_from
        ,(int)model.total_shoot_from,model.total_hit_from * 100.0/model.total_shoot_from],
                        [NSString stringWithFormat:@"%d/%d(%0.1f%%)",(int)model.three_hit_from
                        ,(int)model.three_shoot_home,model.three_hit_from * 100.0/model.three_shoot_from],
                        [NSString stringWithFormat:@"%d/%d(%0.1f%%)",(int)model.penalty_hit_from
                         ,(int)model.penalty_shoot_from,model.penalty_hit_from * 100.0/model.penalty_shoot_from],
                        s(model.REB_from),s(model.AST_from),s(model.STL_from),s(model.BLK_from),s(model.FO_from),s(model.MIS_from)];
                        
    }else{
        self.array7 = @[[NSString stringWithFormat:@"%d/%d(%0.1f%%)",(int)model.total_hit_home
        ,(int)model.total_shoot_home,model.total_hit_home * 100.0/model.total_shoot_home],
                        [NSString stringWithFormat:@"%d/%d(%0.1f%%)",(int)model.penalty_hit_home
                         ,(int)model.penalty_shoot_home,model.penalty_hit_home * 100.0/model.penalty_shoot_home],
                        s(model.AST_home),s(model.STL_home),s(model.FO_home),s(model.MIS_home)];
        self.array8 = @[@"射正/射门",@"命中/点球",@"助攻",@"抢断",@"犯规",@"总失误"];
        
        self.array9 = @[[NSString stringWithFormat:@"%d/%d(%0.1f%%)",(int)model.total_hit_from
        ,(int)model.total_shoot_from,model.total_hit_from * 100.0/model.total_shoot_from],
                        [NSString stringWithFormat:@"%d/%d(%0.1f%%)",(int)model.penalty_hit_from
                         ,(int)model.penalty_shoot_from,model.penalty_hit_from * 100.0/model.penalty_shoot_from],s(model.AST_from),s(model.STL_from),s(model.FO_from),s(model.MIS_from)];
    }
    [self.collectionView1 reloadData];
    [self.collectionView2 reloadData];
    [self.collectionView3 reloadData];

}
+ (NSString *)s:(NSInteger)num{
    return [NSString stringWithFormat:@"%d",(int)num];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    if ([collectionView isEqual: self.collectionView1]) {
        return 3;
    }else if ([collectionView isEqual:self.collectionView2]){
        return 3;
    }else if ([collectionView isEqual:self.collectionView3]){
        return 3;
    }
    return  0;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if ([collectionView isEqual: self.collectionView1]) {
        return 5;
    }else if ([collectionView isEqual:self.collectionView2]){
        return 3;
    }else if ([collectionView isEqual:self.collectionView3]){
        return self.model.isbasketball ? 9 : 6;
    }
    return  0;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    RankItem * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[RankItem ID] forIndexPath:indexPath];
    cell.content.text = @"";
    if ([collectionView isEqual: self.collectionView1]) {
        cell.bgHexColor = indexPath.section == 0 ? @"#2756CA":@"#FFFFFF";
        cell.content.textHexColor = indexPath.section == 0 ? @"#FFFFFF":@"#262729";
        if (indexPath.section == 0 && indexPath.row < self.array1.count) {
            cell.content.text = LS(self.array1[indexPath.row]);
        }else if (indexPath.section == 1 && indexPath.row < self.array1.count){
            cell.content.text = LS(self.array2[indexPath.row]);
        }else if (indexPath.section == 2 && indexPath.row < self.array1.count){
            cell.content.text = LS(self.array3[indexPath.row]);
        }
    }else if ([collectionView isEqual:self.collectionView2]){
        cell.bgHexColor = indexPath.section == 0 ? @"#2756CA":@"#FFFFFF";
        cell.content.textHexColor = indexPath.section == 0 ? @"#FFFFFF":@"#262729";
        if (indexPath.section == 0) {
            cell.content.text = LS(self.array4[indexPath.row]);
        }else if (indexPath.section == 1){
            cell.content.text = LS(self.array5[indexPath.row]);
        }else if (indexPath.section == 2){
            cell.content.text = LS(self.array6[indexPath.row]);
        }
    }else if ([collectionView isEqual:self.collectionView3]){
        cell.backgroundColor = indexPath.section == 1 ? [@"#2756CA" color:0.25]:[@"#FFFFFF" color];
        if (indexPath.section == 0) {
            cell.content.text = LS(self.array7[indexPath.row]);
        }else if (indexPath.section == 1){
            cell.content.text = LS(self.array8[indexPath.row]);
        }else if (indexPath.section == 2){
            cell.content.text = LS(self.array9[indexPath.row]);
        }
    }
    return  cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([collectionView isEqual: self.collectionView1]) {
        CGFloat h = indexPath.section == 0 ? 30 : 26;
        return  CGSizeMake(ceil((SCREEN_WIDTH - 24) / 8.0),h);
    }else if ([collectionView isEqual:self.collectionView2]){
        CGFloat h = indexPath.section == 0 ? 30 : 26;
        return  CGSizeMake(ceil((SCREEN_WIDTH - 24) / 8.0),h);
    }else if ([collectionView isEqual:self.collectionView3]){
        CGFloat h = indexPath.row == 0 ? 30 : 26;
        return  CGSizeMake((SCREEN_WIDTH - 16 - 16) / 3.0,h);
    }
    return  CGSizeZero;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if ([collectionView isEqual: self.collectionView1]) {
        CGFloat bottom = section ==  2 ? 20 : 0;
        return  UIEdgeInsetsMake(0, 0, bottom, 0);
    }else if ([collectionView isEqual:self.collectionView2]){
        CGFloat bottom = section ==  2 ? 20 : 0;
        return  UIEdgeInsetsMake(0, 0, bottom, 0);
    }else if ([collectionView isEqual:self.collectionView3]){
        if (section == 0) {
            return  UIEdgeInsetsMake(0, 8, 20, 0);
        }else if (section == 1){
            return  UIEdgeInsetsMake(0, 0, 0, 0);
        }else{
            return  UIEdgeInsetsMake(0, 0, 20, 8);
        }
    }
    return  UIEdgeInsetsZero;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return  0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

@end
